<template>
  <el-container class="home-container">
    <!-- 侧边栏 -->
    <el-aside :width="isCollapse ? '64px' : '200px'" class="aside">
      <div class="logo-container">
        <img src="@/assets/logo.svg" alt="logo" class="logo-img" v-if="!isCollapse">
        <img src="@/assets/logo-mini.svg" alt="logo" class="logo-img" v-else>
      </div>
      
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical"
        :collapse="isCollapse"
        background-color="#001529"
        text-color="#fff"
        active-text-color="#409EFF"
        router
      >
        <el-menu-item index="/welcome">
          <i class="el-icon-s-home"></i>
          <span slot="title">首页</span>
        </el-menu-item>
        
        <el-submenu index="1">
          <template slot="title">
            <i class="el-icon-s-custom"></i>
            <span>用户管理</span>
          </template>
          <el-menu-item index="/users">用户列表</el-menu-item>
        </el-submenu>
        
        <el-submenu index="2">
          <template slot="title">
            <i class="el-icon-truck"></i>
            <span>车辆管理</span>
          </template>
          <el-menu-item index="/vehicles">车辆列表</el-menu-item>
          <el-menu-item index="/vehicle-brands">车辆品牌</el-menu-item>
          <el-menu-item index="/vehicle-series">车辆系列</el-menu-item>
          <el-menu-item index="/vehicle-usage">车辆使用管理</el-menu-item>
        </el-submenu>
        
        <el-submenu index="3">
          <template slot="title">
            <i class="el-icon-office-building"></i>
            <span>门店管理</span>
          </template>
          <el-menu-item index="/stores">门店列表</el-menu-item>
        </el-submenu>
        
        <el-submenu index="4">
          <template slot="title">
            <i class="el-icon-s-order"></i>
            <span>订单管理</span>
          </template>
          <el-menu-item index="/orders">订单列表</el-menu-item>
        </el-submenu>
        
        <el-submenu index="5">
          <template slot="title">
            <i class="el-icon-document"></i>
            <span>合同管理</span>
          </template>
          <el-menu-item index="/contracts">合同列表</el-menu-item>
        </el-submenu>
        
        <el-submenu index="6">
          <template slot="title">
            <i class="el-icon-picture"></i>
            <span>图片管理</span>
          </template>
          <el-menu-item index="/image-categories">图片分类管理</el-menu-item>
          <el-menu-item index="/image-collections">图片集管理</el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    
    <!-- 右侧内容 -->
    <el-container>
      <!-- 头部 -->
      <el-header class="header">
        <div class="header-left">
          <i 
            :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'" 
            @click="toggleCollapse"
          ></i>
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-if="$route.meta && $route.meta.title">{{ $route.meta.title }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        
        <div class="header-right">
          <el-dropdown trigger="click" @command="handleCommand">
            <span class="user-info">
              <img src="@/assets/avatar.svg" alt="avatar" class="user-avatar">
              {{ userInfo.username || '管理员' }}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="profile">个人中心</el-dropdown-item>
              <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      
      <!-- 主要内容区域 -->
      <el-main class="main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Home',
  data() {
    return {
      isCollapse: false
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
    activeMenu() {
      return this.$route.path
    }
  },
  methods: {
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    handleCommand(command) {
      if (command === 'logout') {
        this.$store.dispatch('logout')
        this.$router.push('/login')
        this.$message.success('退出登录成功')
      } else if (command === 'profile') {
        this.$router.push('/profile')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.home-container {
  height: 100vh;
  
  .aside {
    background-color: #001529;
    transition: width 0.3s;
    overflow: hidden;
    
    .logo-container {
      height: 60px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #002140;
      
      .logo-img {
        height: 32px;
        max-width: 100%;
      }
    }
    
    .el-menu-vertical {
      border-right: none;
      
      &:not(.el-menu--collapse) {
        width: 200px;
      }
    }
  }
  
  .header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
    
    .header-left {
      display: flex;
      align-items: center;
      
      i {
        font-size: 20px;
        cursor: pointer;
        margin-right: 20px;
        color: #606266;
      }
    }
    
    .header-right {
      .user-info {
        display: flex;
        align-items: center;
        cursor: pointer;
        
        .user-avatar {
          width: 30px;
          height: 30px;
          border-radius: 50%;
          margin-right: 8px;
        }
      }
    }
  }
  
  .main {
    background-color: #f0f2f5;
    padding: 20px;
    overflow-y: auto;
  }
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .home-container {
    .header {
      padding: 0 10px;
      
      .header-left {
        i {
          margin-right: 10px;
        }
      }
    }
    
    .main {
      padding: 10px;
    }
  }
}
</style> 